<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/22 0022
 * Time: 11:08
 */

namespace app\admin\controller;


use think\Controller;

class Contact extends Controller
{
    public function update(){
        $data = db("contact")->where("id",1)->find();
        $this->assign([
            'data' => $data
        ]);
        if(request()->post()){
            $id = input("id");
            $contact = db("contact")->where("id",$id)->find();
            $params = request()->except('image');
            $file = request()->file('image');
            if(empty($file)){
                $image = request()->param("oldimage");
            }
            $info=$file->move( './static/images');
            $filename=str_replace("\\","/",$info->getSaveName());
            $image = '/static/images/'.$filename;
            $data = [
                'mobile' => $params['mobile'] ?? $contact['mobile'],
                'email' => $params['email'] ?? $contact['email'],
                'phone' => $params['phone'] ?? $contact['phone'],
                'address' => $params['address'] ?? $contact['address'],
                'name' => $params['name'] ?? $contact['name'],
                'image' => $image,
                'uptime' => date("Y-m-d H:i:s")
            ];
            $res = db("contact")->where("id",1)->update($data);
            if($res){
                return 200;
            }else{
                return -1;
            }
        }
        return view("update");
    }
}